package com.module.aichat.ui.widget.inputview

import android.text.Spannable
import android.view.KeyEvent
import android.widget.EditText
import com.iyao.eastat.KeyCodeDeleteHelper
import com.iyao.eastat.NoCopySpanEditableFactory
import com.iyao.eastat.SpanFactory
import com.iyao.eastat.span.DataBindingSpan
import com.iyao.eastat.watcher.SelectionSpanWatcher
import com.module.aichat.data.bean.ChatUser

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：3/11/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
object AtMemberManager {
    fun init(editText: EditText) {
        editText.text = null
        editText.setEditableFactory(
            NoCopySpanEditableFactory(
            SelectionSpanWatcher(
                DataBindingSpan::class)
            )
        )
        editText.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN) {
                KeyCodeDeleteHelper.onDelDown((v as EditText).text)
            }
            return@setOnKeyListener false
        }
    }

    fun newSpannable(user: ChatUser): Spannable {
        return SpanFactory.newSpannable("@${user.name} ", user)
    }

}

